import { Rectangle } from "../math";
import { Behaviour } from "./Behaviour";
import { getResource } from "./GameEngine";
import { Serialized } from "./Serialized";
import { AbstractRenderer } from "./AbstractRenderer";

export class BitmapRenderer extends Behaviour implements AbstractRenderer {

    @Serialized('textInput')
    source: string;
    bounds: Rectangle = new Rectangle();

    @Serialized('numberInput')
    offsetX: number = 0;

    @Serialized('numberInput')
    offsetY: number = 0;

    getBounds() {

        const img = getResource(this.source);

        if (img) {
            this.bounds.width = img.width;
            this.bounds.height = img.height;
            this.bounds.x = this.offsetX;
            this.bounds.y = this.offsetY;
        }
        return this.bounds;
    }

    render(context: CanvasRenderingContext2D) {

        const img = getResource(this.source);

        if (img) {
            context.drawImage(img, this.offsetX, this.offsetY);
        }
    }
}
